/*
 * @Author: lande 615542329@qq.com
 * @Date: 2023-02-15 14:05:22
 * @LastEditors: lande 615542329@qq.com
 * @LastEditTime: 2023-02-16 14:22:25
 * @FilePath: \ScriptProjects\DiyFunction\diy_module.js
 * @Description: diy_module
 */

const DiyModule = {};

/**
 * @description: 点击控件坐标
 * @param {Object} widget 控件对象
 * @return {Boolean} 控件不存在返回false，点击失败返回false，点击成功返回true
 */
DiyModule.clickWidget = function (widget) {
    try {
        if (!widget || !widget.visibleToUser()) {
            return false;
        }
        const x = random(widget.bounds().left + 1, widget.bounds().right - 1);
        const y = random(widget.bounds().top + 1, widget.bounds().bottom - 1);
        const time = random(150, 280);
        return press(x, y, time);
    } catch (error) {
        console.log('点击控件坐标报错：', error);
    }
    return false;

}

/**
 * @description: 点击控件
 * @param {Object} widget 控件对象
 * @return {Boolean} 控件不存在返回false，点击失败返回false，点击成功返回true
 */
DiyModule.clickWidgetEx = function (widget) {
    try {
        if (!widget || !widget.visibleToUser()) {
            return false;
        }
        return widget.click();
    } catch (error) {
        console.log('点击控件报错：', error);
    }
    return false;

}

/**
 * @description: sleep函数的增强函数
 * @param {Number} time1 时间1 这个参数不传，将默认使用500ms。
 * @param {Number} time2 时间2 这个参数不传，将默认使用time1的1.3倍数值。
 * @return {*} void 
 */
DiyModule.sleepEx = function (time1, time2) {
    try {
        time1 = typeof time1 == 'undefined' ? 500 : time1;
        time2 = typeof time2 == 'undefined' ? parseInt(time1 * 1.3) : time2;
        sleep(random(time1, time2));
    } catch (error) {
        console.log('延迟Ex报错: ', error);
    }
}

/**
 * @description: 判断控件是否处于安全边界内
 * @param {Object} widget 控件对象
 * @param {*} returnType 返回值类型，填object返回坐标对象{left:123,top:456,right:789,bottom:012},控件不在安全边界或不填返回Boolean类型的值，
 * @return {Boolean} Boolean
 */
DiyModule.widgetBounds = function (widget, returnType) {
    try {
        if (!widget || !widget.visibleToUser()) {
            return false;
        }
        const bool = widget.bounds().left < widget.bounds().right && widget.bounds().left >= 0 && widget.bounds().top < widget.bounds().bottom && widget.bounds().top >= 0;
        if (bool && returnType == 'object') {
            return {
                left: widget.bounds().left,
                top: widget.bounds().top,
                right: widget.bounds().right,
                bottom: widget.bounds().bottom
            };
        }
        return bool;

    } catch (error) {
        console.log('控件安全边界报错: ', error);
    }
    return false;
}


/**
 * @description: 仿真随机带曲线滑动, qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
 * @param {Number} qx
 * @param {Number} qy
 * @param {Number} zx
 * @param {Number} zy
 * @param {Number} time
 * @return {*}
 */
DiyModule.sml_move = function (qx, qy, zx, zy, time) {

    /**
    * @description: 随机曲线算法
    * @param {ArrayList} cp 这是一个数组,用来传入位置参数
    * @param {Number} t 时间
    * @return {*}
    */
    function bezier_curves(cp, t) {
        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;

        tSquared = t * t;
        tCubed = tSquared * t;
        result = {
            "x": 0,
            "y": 0
        }
        result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        return result;
    }

    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    }

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }

    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    // log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    // log(xxy);
    gesture.apply(null, xxy);

}


/**
 * @description: 仿真随机带曲线滑动 (梦境加强版)
 * @param {*}
 * @return {*}
 */
DiyModule.smlMoveUp = function () {
    let rndNum = random(1, 5);
    let dw = device.width;
    let dh = device.height;
    switch (rndNum) {
        case 1:
            DiyModule.sml_move(parseInt(dw * 0.42), parseInt(dh * 0.82), parseInt(dw * 0.36), parseInt(dh * 0.18), 1206);
            break;
        case 2:
            DiyModule.sml_move(parseInt(dw * 0.55), parseInt(dh * 0.75), parseInt(dw * 0.83), parseInt(dh * 0.15), 1170);
            break;
        case 3:
            DiyModule.sml_move(parseInt(dw * 0.76), parseInt(dh * 0.7), parseInt(dw * 0.9), parseInt(dh * 0.18), 926);
            break;
        case 4:
            DiyModule.sml_move(parseInt(dw * 0.69), parseInt(dh * 0.73), parseInt(dw * 0.42), parseInt(dh * 0.16), 807);
            break;
        case 5:
            DiyModule.sml_move(parseInt(dw * 0.48), parseInt(dh * 0.76), parseInt(dw * 0.69), parseInt(dh * 0.15), 1305);
            break;
    }
}

/**
 * @description: 获取当前控件的指定同级控件
 * @param {Object} widget 当前控件
 * @param {Number} index 指定的第几个子控件，从0开始计算。
 * @return {Object} 返回 指定的同级控件
 */
DiyModule.getLevel = function (widget, index) {
    try {
        let parentWidget = widget && widget.parent();
        let childCount = parentWidget && parentWidget.childCount();
        return childCount > index ? parentWidget.child(index) : null;

    } catch (error) {
        log("获取当前控件的同级控件报错:", error);
    }
}

/**
 * @description: 传入控件对象的参数，查找控件，并返回控件
 * @param {Object} object 控件对象属性，例：{text:"hello"}
 * @param {String} type 传入 t,i,c,tc,ti,tic其中一个字符串，代表使用的查找方式。
 * @return {Object} Object
 */
DiyModule.findWidget = function (object, type) {
    try {
        type = type == "ct" ? "tc" : type;
        type = type == "it" ? "ti" : type;
        type = type.indexOf("t") > -1 && type.indexOf("i") > -1 && type.indexOf("t") > -1 ? "tic" : type;
        switch (type) {
            case "t":
                return text(object.text).findOnce() || desc(object.text).findOnce();
            case "i":
                return id(object.id).findOnce();
            case "c":
                return className(object.className).findOnce();
            case "tc":
                return text(object.text).className(object.className).findOnce() || desc(object.text).className(object.className).findOnce();
            case "ti":
                return text(object.text).id(object.id).findOnce() || desc(object.text).id(object.id).findOnce();
            case "tic":
                return text(object.text).id(object.id).className(object.className).findOnce() || desc(object.text).id(object.id).className(object.className).findOnce();

        }
    } catch (error) {
        console.log("查找控件报错: ", error);
    }
    return null;
}



module.exports = DiyModule;